const app = new Vue({
    el: '#registerBox',
    data: function () {
        return {
            info: {
                username: '',
                firstPwd: '',
                secondPwd: ''
            },
            account: {
                username: '',
                pwd: ''
            }
        }
    },
    methods: {
        cancelReg: function () {
            this.info = {
                username: '',
                firstPwd: '',
                secondPwd: ''
            }
        },
        register: function () {
            if (this.info.username === '') {
                this.$message.warning('请填写用户名！');
                return;
            }
            if (this.info.firstPwd === '') {
                this.$message.warning('请填写密码！');
                return;
            }
            if (this.info.secondPwd === '') {
                this.$message.warning('请填写确认密码！');
                return;
            }

            if (this.info.firstPwd !== this.info.secondPwd) {
                this.$message.error('密码和确认密码应相同，请重新填写！');
                this.info.firstPwd = '';
                this.info.secondPwd = '';
                return;
            }

            this.account.username = this.info.username;
            this.account.pwd = this.info.firstPwd;

            let that = this;
            axios.post("/demo/register", that.account).then(
                function (res) {
                    let status = res.status;
                    if (status === 200) {
                        let data = res.data;
                        if (data === 200) {
                            that.$message.success('注册成功!');
                            that.cancelReg();
                        } else if (data === 10001) {
                            that.$message.warning('用户名和密码不能为空!');
                        }
                    } else {
                        console.log(res);
                    }
                    that.account = {
                        username: '',
                        pwd: ''
                    }
                }, function (err) {
                    console.log(err);
                }
            )
        }
    },
});